package org.web.project.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.utils.AjaxResult;
import org.web.project.domain.SysUser;
import org.web.project.service.SysUserService;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.utils.AjaxResult.error;

@RestController
@RequestMapping("/user/*")
public class UserController {
    @Autowired
    private SysUserService userService;
    /**
     * 获取用户列表
     */
    @GetMapping("list")
    public AjaxResult list(SysUser user)
    {
        return AjaxResult.success(userService.selectUserList(user));
    }
    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        return AjaxResult.success(userService.selectUserById(userId));
    }
    /**
     * 新增用户
     */
    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody SysUser user)
    {
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (ObjectUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (ObjectUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        user.setCreateBy("tang");
        return userService.save(user)?AjaxResult.success("添加用户成功",user):AjaxResult.error("添加用户失败");
    }

    /**
     * 修改用户
     */

    @PutMapping("update")
    public AjaxResult edit(@Validated @RequestBody SysUser user)
    {
        if (!userService.checkUserNameUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (ObjectUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (ObjectUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        return userService.updateById(user)?AjaxResult.success("修改用户成功",user):AjaxResult.error("修改用户失败");
    }

    /**
     * 删除用户
     */
    @DeleteMapping("{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return userService.removeBatchByIds(Arrays.asList(userIds))?AjaxResult.success("删除用户成功"):AjaxResult.error("删除用户失败");
    }

    /**
     * 重置密码
     */
    @PutMapping("resetPwd")
    public AjaxResult resetPwd(@RequestBody SysUser user)
    {
        user.setCreateBy("tang");
        return userService.updateById(user)?AjaxResult.success("修改用户密码成功",user):AjaxResult.error("修改用户密码失败");
    }

    /**
     * 状态修改
     */

    @PutMapping("changeStatus")
    public AjaxResult changeStatus(@RequestBody SysUser user)
    {
        user.setCreateBy("tang");
        return userService.updateById(user)?AjaxResult.success("修改用户状态成功",user):AjaxResult.error("修改用户状态失败");
    }
}
